package com.fun_ordering.service.impl;

import com.fun_ordering.service.MarketingActivityService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.fun_ordering.components.PageResult;
import com.fun_ordering.mapper.MarketingActivityMapper;
import com.fun_ordering.pojo.MarketingActivity;
import com.fun_ordering.pojo.MarketingActivityExample;
import com.fun_ordering.pojo.MarketingActivityExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 营销活动表服务实现层
 * @author XiaoPi
 *
 */
@Service
public class MarketingActivityServiceImpl implements MarketingActivityService {

	@Autowired
	private MarketingActivityMapper marketingActivityMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<MarketingActivity> findAll() {
		return marketingActivityMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<MarketingActivity> page=   (Page<MarketingActivity>) marketingActivityMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(MarketingActivity marketingActivity) {
		marketingActivityMapper.insert(marketingActivity);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(MarketingActivity marketingActivity){
		marketingActivityMapper.updateByPrimaryKey(marketingActivity);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param actId
	 * @return
	 */
	@Override
	public MarketingActivity findOne(Integer actId){
		return marketingActivityMapper.selectByPrimaryKey(actId);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] actIds) {
		for(Integer actId:actIds){
			marketingActivityMapper.deleteByPrimaryKey(actId);
		}		
	}
	
	
		@Override
	public PageResult findPage(MarketingActivity marketingActivity, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		MarketingActivityExample example=new MarketingActivityExample();
		Criteria criteria = example.createCriteria();
		
		if(marketingActivity!=null){			
						if(marketingActivity.getActName()!=null && marketingActivity.getActName().length()>0){
				criteria.andActNameLike("%"+marketingActivity.getActName()+"%");
			}			if(marketingActivity.getActDes()!=null && marketingActivity.getActDes().length()>0){
				criteria.andActDesLike("%"+marketingActivity.getActDes()+"%");
			}			if(marketingActivity.getDiscountInfo()!=null && marketingActivity.getDiscountInfo().length()>0){
				criteria.andDiscountInfoLike("%"+marketingActivity.getDiscountInfo()+"%");
			}	
		}
		
		Page<MarketingActivity> page= (Page<MarketingActivity>)marketingActivityMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
